package top.zsyle.movie.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.zsyle.movie.domain.MovieTicket;
import top.zsyle.movie.domain.UserMovie;
import top.zsyle.movie.dto.GetMiddleDto;
import top.zsyle.movie.query.MovieTicketQuery;
import top.zsyle.movie.result.JsonResult;
import top.zsyle.movie.result.PageList;
import top.zsyle.movie.service.IMovieTicketService;
import top.zsyle.movie.service.IUserMovieService;

import javax.validation.Valid;

@RestController
@RequestMapping("/movieTicket")
public class MovieTicketController {

    @Autowired
    public IMovieTicketService movieTicketService;

    @Autowired
    private IUserMovieService userMovieService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody MovieTicket movieTicket){
        if(movieTicket.getId()!=null){
            movieTicketService.updateById(movieTicket);
        }else{
            movieTicketService.insert(movieTicket);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        movieTicketService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(movieTicketService.selectById(id));
    }

    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(movieTicketService.selectList(null));
    }

    @RequestMapping(value = "/listNotOverTime",method = RequestMethod.GET)
    public JsonResult listNotOverTime(Wrapper<MovieTicket> wrapper){
        return JsonResult.success(movieTicketService.selectList(wrapper));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody MovieTicketQuery query){
        Page<MovieTicket> page = new Page<MovieTicket>(query.getPage(),query.getRows());
        page = movieTicketService.selectPage(page);
        return JsonResult.success(new PageList<MovieTicket>(page.getTotal(),page.getRecords()));
    }



    @RequestMapping(value = "/userMovie/{id}",method = RequestMethod.GET)
    public JsonResult getUserMovie(@PathVariable("id") Long id){
        Wrapper<UserMovie> wrapper = new EntityWrapper<>();
        Wrapper<UserMovie> userMovieWrapper = wrapper.eq("movie_ticket_id", id);
        UserMovie userMovie = userMovieService.selectOne(userMovieWrapper);
        return JsonResult.success(userMovie);
    }

    //
    @RequestMapping(value = "/getMiddleByUserIdAndMovieId",method = RequestMethod.GET)
    public JsonResult getMiddleByUserIdAndMovieId(@RequestBody @Valid GetMiddleDto dto){
        return JsonResult.success(userMovieService.getMiddleByUserIdAndMovieId(dto));
    }

    /**
     * 保存和修改中间表
     */
    @RequestMapping(value="/saveMiddle",method= RequestMethod.POST)
    public JsonResult saveOrUpdateMiddle(@RequestBody UserMovie userMovie){
        if(userMovie.getId()!=null){
            userMovieService.updateById(userMovie);
        }else{
            userMovieService.insert(userMovie);
        }
        return JsonResult.success();
    }

}
